package com.swe.chums.dao.impl;

import com.swe.chums.dao.UserTagTypeDao;
import com.swe.chums.dao.model.UserTagType;
import com.swe.chums.dao.model.UserTagTypePk;
import com.swe.chums.dao.exceptions.UserTagTypeDaoException;
import java.util.List;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.transaction.annotation.Transactional;

public class UserTagTypeDaoImpl extends AbstractDAO implements ParameterizedRowMapper<UserTagType>, UserTagTypeDao
{
	protected SimpleJdbcTemplate jdbcTemplate;

	protected DataSource dataSource;

	/**
	 * Method 'setDataSource'
	 * 
	 * @param dataSource
	 */
	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return UserTagTypePk
	 */
	@Transactional
	public UserTagTypePk insert(UserTagType dto)
	{
		jdbcTemplate.update("INSERT INTO " + getTableName() + " ( user_id, tag_id ) VALUES ( ?, ? )",dto.getUserId(),dto.getTagId());
		UserTagTypePk pk = new UserTagTypePk();
		pk.setId( jdbcTemplate.queryForInt("select last_insert_id()") );
		return pk;
	}

	/** 
	 * Updates a single row in the user_tag_type table.
	 */
	@Transactional
	public void update(UserTagTypePk pk, UserTagType dto) throws UserTagTypeDaoException
	{
		jdbcTemplate.update("UPDATE " + getTableName() + " SET id = ?, user_id = ?, tag_id = ? WHERE id = ?",dto.getId(),dto.getUserId(),dto.getTagId(),pk.getId());
	}

	/** 
	 * Deletes a single row in the user_tag_type table.
	 */
	@Transactional
	public void delete(UserTagTypePk pk) throws UserTagTypeDaoException
	{
		jdbcTemplate.update("DELETE FROM " + getTableName() + " WHERE id = ?",pk.getId());
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return UserTagType
	 */
	public UserTagType mapRow(ResultSet rs, int row) throws SQLException
	{
		UserTagType dto = new UserTagType();
		dto.setId( rs.getInt( 1 ) );
		dto.setUserId( rs.getInt( 2 ) );
		dto.setTagId( rs.getInt( 3 ) );
		return dto;
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "spring2013g3.user_tag_type";
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria 'id = :id'.
	 */
	@Transactional
	public UserTagType findByPrimaryKey(int id) throws UserTagTypeDaoException
	{
		try {
			List<UserTagType> list = jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE id = ?", this,id);
			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria ''.
	 */
	@Transactional
	public List<UserTagType> findAll() throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " ORDER BY id", this);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria 'user_id = :userId'.
	 */
	@Transactional
	public List<UserTagType> findByUser(int userId) throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE user_id = ?", this,userId);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria 'tag_id = :tagId'.
	 */
	@Transactional
	public List<UserTagType> findByTagType(int tagId) throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE tag_id = ?", this,tagId);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria 'id = :id'.
	 */
	@Transactional
	public List<UserTagType> findWhereIdEquals(int id) throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE id = ? ORDER BY id", this,id);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria 'user_id = :userId'.
	 */
	@Transactional
	public List<UserTagType> findWhereUserIdEquals(int userId) throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE user_id = ? ORDER BY user_id", this,userId);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the user_tag_type table that match the criteria 'tag_id = :tagId'.
	 */
	@Transactional
	public List<UserTagType> findWhereTagIdEquals(int tagId) throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE tag_id = ? ORDER BY tag_id", this,tagId);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}

        /** 
	 * Returns all rows from the user_tag_type table that match the criteria 'tag_id = :tagId'.
	 */
	@Transactional
	public List<UserTagType> findWhereUserIdandTagIdEquals(int userId, int tagId) throws UserTagTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, user_id, tag_id FROM " + getTableName() + " WHERE tag_id = ? and  user_id = ? ORDER BY id",this,tagId,userId);
		}
		catch (Exception e) {
			throw new UserTagTypeDaoException("Query failed", e);
		}
		
	}
	/** 
	 * Returns the rows from the user_tag_type table that matches the specified primary-key value.
	 */
	public UserTagType findByPrimaryKey(UserTagTypePk pk) throws UserTagTypeDaoException
	{
		return findByPrimaryKey( pk.getId() );
	}

}
